
package com.xwyzsn.rentcar.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xwyzsn.rentcar.entity.Rentorder;
import com.xwyzsn.rentcar.mapper.RentorderMapper;
import com.xwyzsn.rentcar.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.lang.reflect.Field;
import java.util.Date;

@Service
public class RentorderService  {

    @Autowired
    private RentorderMapper rentorderMapper;
    final private  DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public List<Rentorder> getOne(Map<String,?>payload){
        if (payload.isEmpty()){
            System.out.println("get all");
            return rentorderMapper.selectList(null);
        }
        Integer id = (Integer) payload.get("id");
        return rentorderMapper.getOne(id);
    }
    public Result getOrderByUserId(Map<String,?>payload){
        if (payload.isEmpty()){
            return Result.fail("userId is required");
        }
        Integer userId = Integer.parseInt(String.valueOf(payload.get("userId")));
        QueryWrapper<Rentorder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        List<Rentorder> rentorders = rentorderMapper.selectList(queryWrapper);
        return Result.succ(rentorders);
    }

    public int addOne(Map<String,?>payload) throws ParseException {
        if (payload.isEmpty())return 0;
        Integer userId = Integer.parseInt(String.valueOf(payload.get("userId")));
        Integer carId = Integer.parseInt(String.valueOf(payload.get("carId")));
        Date startTime = dateFormat.parse((String) payload.get("startTime"));
        Date endTime = dateFormat.parse((String) payload.get("endTime"));
        Double price = Double.parseDouble(String.valueOf(payload.get("price")));
        Rentorder rentorder = new Rentorder();
        rentorder.setUserId(userId);
        rentorder.setCarId(carId);
        rentorder.setStartTime(startTime);
        rentorder.setEndTime(endTime);
        rentorder.setPrice(price);
        return rentorderMapper.insert(rentorder);
    }

    public Boolean deleteOne(Map<String,?>payload){
        Integer id = (Integer) payload.get("id");
        return rentorderMapper.deleteOne(id);
    }

    public Integer updateOne(Map<String,?>payload){
        Rentorder rentorder = setEntity(payload,Rentorder.class);
        return rentorderMapper.updateOne(rentorder);
    }

    public static <T> T setEntity(Map<String, ?> payload, Class<T> clazz) {
        try {
            T entity = clazz.newInstance();
            Field[] fields = clazz.getDeclaredFields();

            for (Field field : fields) {
                String fieldName = field.getName();
                if (payload.containsKey(fieldName)) {
                    field.setAccessible(true);
                    Object value = payload.get(fieldName);
                    // Handle type conversions if necessary
                    if (field.getType() == Integer.class ) {
                        value = Integer.valueOf((String) value);
                    } else if (field.getType() == Double.class ) {
                        value = Double.valueOf((String) value);
                    } else if (field.getType() == Date.class) {
                        value = new Date();
                    }else if (field.getType() == String.class ){
                        value = (String) value;
                    }
                    field.set(entity, value);
                }
            }
            return entity;
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace(); // Handle or log the exception as needed
            return null;
        }
    }
}

